package com.xujl.fastlib.base;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xujl.fastlib.R;
import com.xujl.fastlib.utils.LogS;
import com.xujl.fastlib.utils.ResUtils;
import com.xujl.fastlib.utils.StringUtils;
import com.xujl.fastlib.utils.ToastUtils;
import com.xujl.fastlib.window.LoadingWindow;

import java.util.Arrays;
import java.util.LinkedList;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

/**
 * @author xujl
 * @email 597355068@qq.com
 * @Date on 2019/6/18
 * @Description
 */
public abstract class BaseView<B extends ViewDataBinding> implements LifecycleObserver {
    private static final String TAG = "BaseView";
    protected ViewGroup mRootView;
    protected View mContentView;
    protected ViewGroup mContentRoot;
    protected B mBind;
    private ViewDataBinding mToolbarBind;
    private LayoutBuilder mLayoutBuilder;
    private LoadingWindow mLoadingWindow;

    public BaseView () {
        mLayoutBuilder = configLayout();
    }

    public abstract void init (IControl control);

    /**
     * 布局属性配置
     *
     * @return
     */
    public LayoutBuilder configLayout () {
        return LayoutBuilder.create().build();
    }

    public void toast (String msg) {
        ToastUtils.toast(msg);
    }

    public View createLayout (LayoutInflater inflater, int layoutId) {
        mRootView = (ViewGroup) inflater.inflate(R.layout.base_layout, null);
        /*
        不使用多状态布局，不使用导航栏，不强制添加父布局时，将会删除多余嵌套层级
         */
        if (!mLayoutBuilder.isUseStateLayout() && mLayoutBuilder.getToolbarId() == 0 &&
                !mLayoutBuilder.isAddParentLayout()) {
            mRootView.removeAllViews();
            //此时，根布局就是内容布局
            mContentRoot = mRootView;
            mContentView = inflater.inflate(layoutId, null);
            mContentRoot.addView(mContentView, 0);
            if (mLayoutBuilder.isEnableDataBinding()) {
                mBind = DataBindingUtil.bind(mContentView);
            }
            return mRootView;
        }
        mContentRoot = mRootView.findViewById(R.id.root_content);
        mContentView = inflater.inflate(layoutId, null);
        mContentRoot.addView(mContentView, 0);
        if (mLayoutBuilder.isEnableDataBinding()) {
            mBind = DataBindingUtil.bind(mContentView);
        }
        if (mLayoutBuilder.isUseStateLayout()) {
            mLayoutBuilder.setStateView((IStateLayout) mRootView.findViewById(R.id.state_layout));
        }
        if (mLayoutBuilder.getToolbarId() != 0) {
            if (mLayoutBuilder.isEnableToolbarDataBinding()) {
                mToolbarBind = DataBindingUtil.bind(inflater.inflate(mLayoutBuilder.getToolbarId(), null));
                mRootView.addView(mToolbarBind.getRoot(), 0);
            } else {
                mRootView.addView(inflater.inflate(mLayoutBuilder.getToolbarId(), null), 0);
            }
        }
        return mRootView;
    }

    public View createLayout (Activity activity, String clsName) {
        createLayout(LayoutInflater.from(activity), autoCreateLayoutId("activity", clsName));
        return mRootView;

    }

    public View createLayout (LayoutInflater inflater, ViewGroup container, String clsName) {
        createLayout(inflater, autoCreateLayoutId("fragment", clsName));
        return mRootView;
    }

    void setRootView (ViewGroup rootView) {
        mRootView = rootView;
    }

    /**
     * 自动查找布局id
     *
     * @return
     */
    private int autoCreateLayoutId (String type, String clsName) {
        int layoutId = mLayoutBuilder.getLayoutId();
        if (layoutId != 0) {
            return layoutId;
        }
        LogS.i(TAG, "autoCreateLayoutId:" + clsName);
        String toLine = StringUtils.humpToLine(clsName);
        String[] split = toLine.split("_");
        LinkedList<String> splitList = new LinkedList(Arrays.asList(split));
        splitList.removeFirst();
        splitList.removeLast();
        splitList.removeLast();
        splitList.addFirst(type);
        StringBuilder layoutSb = new StringBuilder();
        for (String s : splitList) {
            layoutSb.append(s);
            layoutSb.append("_");
        }
        layoutSb.deleteCharAt(layoutSb.length() - 1);
        return ResUtils.getLayoutResForString(layoutSb.toString());
    }

    public void showState (@IStateLayout.StateType int type) {
        mLayoutBuilder.getStateView().showState(type);
    }

    public <T extends View> T findViewById (int id) {
        return mRootView.findViewById(id);
    }

    public <T extends ViewDataBinding> T getToolbarBinding () {
        return (T) mToolbarBind;
    }

    public void bindData (int id, Object data) {
        if (mBind == null) {
            return;
        }
        mBind.setVariable(id, data);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny (LifecycleOwner owner, Lifecycle.Event event) {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate () {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy () {
        if (mLoadingWindow != null) {
            mLoadingWindow.recycle();
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart () {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop () {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume () {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause () {
    }

    public B getBind () {
        return mBind;
    }

    public LoadingWindow showLoadingWindow () {
        if (mLoadingWindow != null) {
            mLoadingWindow.showPopupWindow();
            return mLoadingWindow;
        }
        mLoadingWindow = new LoadingWindow(mRootView.getContext());
        mLoadingWindow.showPopupWindow();
        return mLoadingWindow;
    }

    public LoadingWindow dismissLoadingWindow () {
        if (mLoadingWindow != null) {
            mLoadingWindow.dismiss();
            return mLoadingWindow;
        }
        return null;
    }
}
